package com.daocao.ds.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.daocao.ds.R;
import com.daocao.ds.api.Api;
import com.daocao.ds.api.bean.ModifyCheckCodeBean;
import com.daocao.ds.api.bean.ModifySendBean;
import com.daocao.ds.base.BaseActivity;
import com.daocao.ds.constant.UserInfoGlobal;
import com.daocao.ds.event.SetPayPassSuccessEvent;
import com.daocao.ds.util.Network;
import com.daocao.ds.util.RxLifeUtil;
import com.daocao.ds.util.ToastUtil;
import com.daocao.ds.util.UI;
import com.daocao.ds.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class TransationSetStepOneActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.btn_send_code)
    TextView btnSendCode;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;

    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaction_set_one);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
        EventBus.getDefault().register(this);
    }

    private void init() {
        txtTitle.setText("交易密码");
        txtPhone.setText(UserInfoGlobal.getUserPhone());

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(SetPayPassSuccessEvent event) {
        finish();
    }

    private CountDownTimer timer = new CountDownTimer(60000L, 1000L) {
        public void onFinish() {
            resetBtn();
        }

        @TargetApi(16)
        public void onTick(long paramAnonymousLong) {
            btnSendCode.setEnabled(false);
            btnSendCode.setTextColor(0xff999999);
            btnSendCode.setText(paramAnonymousLong / 1000L + "s重新获取");
        }
    };

    @TargetApi(16)
    private void resetBtn() {
        if (this.timer != null)
            this.timer.cancel();

        //        if (this.isSend)
        //            this.btnSendCode.setText(getResources().getString(R.string.send_code_again));
        //        else
        btnSendCode.setText("获取验证码");
        btnSendCode.setTextColor(0xff0072FF);
        btnSendCode.setEnabled(true);
    }

    public void getSmsSend(String phone) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        ModifySendBean item = new ModifySendBean();
        item.phone = phone;

        loadingDialog.show();
        Api.api_service.getSmsSend(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "发送成功");
                    timer.start();
                });
    }

    public void getSmsCheck(String phone, String verify_code) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        ModifyCheckCodeBean item = new ModifyCheckCodeBean();
        item.phone = phone;
        item.verify_code = verify_code;
        loadingDialog.show();
        Api.api_service.getSmsCheck(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "验证成功");
                    Intent intent = new Intent(this, TransationSetStepTwoActivity.class);
                    intent.putExtra("verify_token", result.datas.verify_token);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (this.timer != null)
            this.timer.cancel();
        timer = null;
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_send_code, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_send_code:
                if (TextUtils.isEmpty(UI.toString(txtPhone))) {
                    ToastUtil.makeText(this, "手机号不能为空");
                    return;
                }
                getSmsSend(UI.toString(txtPhone));
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editCode))) {
                    ToastUtil.makeText(this, "验证码不能为空");
                    return;
                }

                getSmsCheck(UI.toString(txtPhone), UI.toString(editCode));
                break;
        }
    }
}
